package com.zhangkunhao.notes.service;

import com.zhangkunhao.notes.dao.TopicDao;
import com.zhangkunhao.notes.entity.Do.*;

import com.zhangkunhao.notes.entity.bo.PageBean;
import com.zhangkunhao.notes.service.impl.TopicService;

import java.sql.SQLException;
import java.util.*;
import java.util.stream.Collectors;


/**
 * 话题接口的实现类
 *
 * @author 20642
 */
public class TopicServiceImpl implements TopicService {

    private TopicPursueListServiceImpl topicPursueListService = new TopicPursueListServiceImpl();

    @Override
    public List<Topic> selectAll() {
        return TopicDao.getInstance().findAll();
    }

    @Override
    public void insert(Topic topic) throws Exception {
        TopicDao.getInstance().insert(topic);
    }

    @Override
    public int delete(int id) throws SQLException {
        return TopicDao.getInstance().delete(id);
    }

    @Override
    public int update(Topic topic, String... params) throws Exception {
        return TopicDao.getInstance().update(topic);
    }

    public PageBean<Topic> showNotPursueTopics(int userId) {
        //初始化
        PageBean<Topic> topicPageBean = new PageBean<>(0, null);
        //调用dao查询所有话题
        List<Topic> allTopics = this.selectAll();
        //调用dao查询我已经关注的话题
        List<TopicPursueList> topicPursueLists = topicPursueListService.selectAll()
                .stream()
                .filter(topicPursueList -> topicPursueList.getFollowerId().equals(userId))
                .collect(Collectors.toList());

        // 迭代器,过滤关注过的话题
        Iterator<Topic> it = allTopics.iterator();
        while (it.hasNext()) {
            Topic next = it.next();
            for (TopicPursueList topic : topicPursueLists) {
                if (next.getId().equals(topic.getTopicId())) {
                    it.remove();
                    break;
                }
            }
        }

        //返回未关注的话题列表
        if (!allTopics.isEmpty()) {
            topicPageBean.setTotalCount(allTopics.size());
            topicPageBean.setRows(allTopics);
        }
        //返回数据
        return topicPageBean;
    }

    public PageBean<Topic> showPursueTopics(int userId) {
        //初始化
        PageBean<Topic> topicPageBean = new PageBean<>(0, null);
        //查询我已经关注的话题
        List<TopicPursueList> topicPursueLists = topicPursueListService.selectAll()
                .stream()
                .filter(topicPursueList -> topicPursueList.getFollowerId().equals(userId))
                .collect(Collectors.toList());
        //我关注的话题的集合
        ArrayList<Topic> topics = new ArrayList<>();
        if (!topicPursueLists.isEmpty()) {
            for (TopicPursueList topicPursue : topicPursueLists) {
                //添加到集合中
                topics.add(new Topic(topicPursue.getTopicId(), topicPursue.getTopicName()));
            }
        } else {
            return topicPageBean;
        }

        //返回关注的话题列表
        if (topics.size() > 0) {
            topicPageBean.setTotalCount(topics.size());
            topicPageBean.setRows(topics);
        }
        //返回数据
        return topicPageBean;
    }
}
